package cn.may.easyexcel;

import cn.may.entity.Phone;
import cn.may.entity.Student;
import cn.may.mybatisplus.repository.StudentDao;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import jdk.internal.util.xml.impl.Input;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author May
 * @date 2020/10/30 11:54
 * @desc
 */
public class EasyExcelUtil {

    /**
     * 读取excel，读取多个不同的sheet
     * @author mayuanbao
     * @date 2020/11/01 15:24
     * @param inputStream inputStream
     * @param clzz clzz
     * @param listeners listeners
     * @return void
     */
    public static String multiRead(InputStream inputStream, List<Class<?>> clzz,
                                   List<AnalysisEventListener<?>> listeners) {
        ExcelReader excelReader = null;
        try {
            excelReader = EasyExcel.read(inputStream).build();
            List<ReadSheet> readSheets = new ArrayList<>();
            // 循环读取所有sheet
            for (int sheetNum = 0; sheetNum < clzz.size(); sheetNum++) {
                ReadSheet readSheet =
                        EasyExcel.readSheet(sheetNum).head(clzz.get(sheetNum))
                                .registerReadListener(listeners.get(sheetNum))
                                .build();
                readSheets.add(readSheet);
            }
            // 这里注意 一定要把所有sheet 一起传进去，不然有个问题就是03版的excel 会读取多次，浪费性能
            excelReader.read(readSheets);
        } finally {
            if (excelReader != null) {
                // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
                excelReader.finish();
            }
        }
        return "success";
    }

    /**
     * 写入excel，这里有多个sheet
     * @author mayuanbao
     * @date 2020/11/01 16:18
     * @param outputStream outputStream
     * @param sheetNames sheetNames
     * @param clzz clzz
     * @param workbookData workbookData
     * @return void
     */
    public static void multiWrite(OutputStream outputStream, List<String> sheetNames, List<Class<?>> clzz,
                                  List<List<?>> workbookData) {
//        fileName = fileName + System.currentTimeMillis() + ".xlsx";
        ExcelWriter excelWriter = null;
        try {
            // 这里 指定文件
            excelWriter = EasyExcel.write(outputStream).build();
            // 根据sheet个数确定写入次数
            for (int i = 0; i < sheetNames.size(); i++) {
                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变。
                // 实际上可以一直变
                WriteSheet writeSheet = EasyExcel.writerSheet(i, sheetNames.get(i)).head(clzz.get(i)).build();
                // 获取工作簿中每个sheet的数据，并写入
                List<?> sheetData = workbookData.get(i);
                excelWriter.write(sheetData, writeSheet);
            }
        } finally {
            // 千万别忘记finish 会帮忙关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }
}
